/* ==================================================================   
 * Created [2009-4-27 下午11:32:55] by Jon.King 
 * ==================================================================  
 * TSS 
 * ================================================================== 
 * mailTo:jinpujun@hotmail.com
 * Copyright (c) Jon.King, 2009-2012 
 * ================================================================== 
 */

package com.jinhe.tss.cms.workflow;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.jinhe.tss.cms.CMSConstants;
import com.jinhe.tss.component.support.web.webwork.PTActionSupport;
import com.jinhe.tss.core.cachepool.CacheableKey;
import com.jinhe.tss.core.web.dispaly.tree.TreeEncoder;
import com.jinhe.tss.core.web.dispaly.xform.XFormEncoder;
 
public class WorkFlowAction extends PTActionSupport {

	private static final long serialVersionUID = -4539854543499272897L;
    
	private File    file;
	private String  name;
	private Integer code;
	
	private WorkFlowService workFlowService;
	
	/**
	 * 导入工作流
	 * @return
	 */
	public String importWorkFlow(){
        WorkFlowManager.getInstance().importWorkFlow(file, name);
		return printSuccessMessage("导入成功!");
	}
	
	/**
	 * 显示工作流
	 * @return
	 */
	public String viewAll(){
        List<Object> list = new ArrayList<Object>();
        List<CacheableKey> codeList = WorkFlowManager.getInstance().getWorkFlowCodes();
        for( CacheableKey code : codeList ){
            list.add(WorkFlowManager.pool.getObject(code).getValue());
        }
		return print("WorkFlowTree", new TreeEncoder(list));
	}
	
	/**
	 * 删除工作流
	 * @return
	 */
	public String delete(){
		workFlowService.deleteWorkFlow(code);
        return printSuccessMessage("删除成功!");
	}
	
	/**
	 * 获取工作流模板
	 * @return
	 */
	public String getWorkFlowModel(){
		String model = WorkFlowManager.getModel();
		WorkFlow workflow = new WorkFlow();
		workflow.setDefinition(model);
		XFormEncoder encoder = new XFormEncoder(CMSConstants.XFORM_TEMPLATE_WORKFLOW, workflow);

		return print("ImportWorkFlowInfo",encoder);
	}

	public void setCode(Integer code) {
		this.code = code;
	}
	public void setFile(File file) {
		this.file = file;
	}
	public void setName(String name) {
		this.name = name;
	}
	public void setWorkFlowService(WorkFlowService service) {
		this.workFlowService = service;
	}
}
